package com.example.redislearning.controller;

import com.example.redislearning.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 消息通知控制器
 * 提供系统公告、用户消息、学习提醒等REST API
 */
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 发布系统公告
     */
    @PostMapping("/announcements")
    public ResponseEntity<Map<String, Object>> publishAnnouncement(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam String publisher) {
        
        boolean success = notificationService.publishAnnouncement(title, content, publisher);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "系统公告发布成功" : "系统公告发布失败"
        ));
    }

    /**
     * 获取系统公告列表
     */
    @GetMapping("/announcements")
    public ResponseEntity<Map<String, Object>> getAnnouncements(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        List<Map<String, Object>> announcements = notificationService.getAnnouncements(page, size);
        
        return ResponseEntity.ok(Map.of(
            "success", true,
            "data", announcements,
            "page", page,
            "size", size,
            "total", announcements.size()
        ));
    }

    /**
     * 发送用户消息
     */
    @PostMapping("/messages/{userId}")
    public ResponseEntity<Map<String, Object>> sendUserMessage(
            @PathVariable Integer userId,
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam(defaultValue = "SYSTEM") String type) {
        
        boolean success = notificationService.sendUserMessage(userId, title, content, type);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "消息发送成功" : "消息发送失败"
        ));
    }

    /**
     * 获取用户消息列表
     */
    @GetMapping("/messages/{userId}")
    public ResponseEntity<Map<String, Object>> getUserMessages(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        List<Map<String, Object>> messages = notificationService.getUserMessages(userId, page, size);
        
        return ResponseEntity.ok(Map.of(
            "success", true,
            "data", messages,
            "page", page,
            "size", size,
            "total", messages.size()
        ));
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/messages/{userId}/{messageId}/read")
    public ResponseEntity<Map<String, Object>> markMessageAsRead(
            @PathVariable Integer userId,
            @PathVariable String messageId) {
        
        boolean success = notificationService.markMessageAsRead(userId, messageId);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "消息已标记为已读" : "消息标记失败"
        ));
    }

    /**
     * 获取用户未读消息数量
     */
    @GetMapping("/messages/{userId}/unread-count")
    public ResponseEntity<Map<String, Object>> getUnreadMessageCount(@PathVariable Integer userId) {
        Integer count = notificationService.getUnreadMessageCount(userId);
        
        return ResponseEntity.ok(Map.of(
            "success", true,
            "count", count
        ));
    }

    /**
     * 发送学习提醒
     */
    @PostMapping("/reminders/study/{userId}/{courseId}")
    public ResponseEntity<Map<String, Object>> sendStudyReminder(
            @PathVariable Integer userId,
            @PathVariable Integer courseId,
            @RequestParam String courseName) {
        
        boolean success = notificationService.sendStudyReminder(userId, courseId, courseName);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "学习提醒发送成功" : "学习提醒发送失败"
        ));
    }

    /**
     * 发送课程更新通知
     */
    @PostMapping("/notifications/course-update/{userId}")
    public ResponseEntity<Map<String, Object>> sendCourseUpdateNotification(
            @PathVariable Integer userId,
            @RequestParam String courseName,
            @RequestParam String updateContent) {
        
        boolean success = notificationService.sendCourseUpdateNotification(userId, courseName, updateContent);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "课程更新通知发送成功" : "课程更新通知发送失败"
        ));
    }

    /**
     * 发送促销通知
     */
    @PostMapping("/notifications/promotion/{userId}")
    public ResponseEntity<Map<String, Object>> sendPromotionNotification(
            @PathVariable Integer userId,
            @RequestParam String promotionTitle,
            @RequestParam String promotionContent) {
        
        boolean success = notificationService.sendPromotionNotification(userId, promotionTitle, promotionContent);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "促销通知发送成功" : "促销通知发送失败"
        ));
    }

    /**
     * 删除用户消息
     */
    @DeleteMapping("/messages/{userId}/{messageId}")
    public ResponseEntity<Map<String, Object>> deleteUserMessage(
            @PathVariable Integer userId,
            @PathVariable String messageId) {
        
        boolean success = notificationService.deleteUserMessage(userId, messageId);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "消息删除成功" : "消息删除失败"
        ));
    }

    /**
     * 批量标记所有消息为已读
     */
    @PutMapping("/messages/{userId}/mark-all-read")
    public ResponseEntity<Map<String, Object>> markAllMessagesAsRead(@PathVariable Integer userId) {
        
        boolean success = notificationService.markAllMessagesAsRead(userId);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "所有消息已标记为已读" : "批量标记失败"
        ));
    }

    /**
     * 清理用户过期消息
     */
    @DeleteMapping("/messages/{userId}/clean-expired")
    public ResponseEntity<Map<String, Object>> cleanExpiredMessages(@PathVariable Integer userId) {
        
        boolean success = notificationService.cleanExpiredMessages(userId);
        
        return ResponseEntity.ok(Map.of(
            "success", success,
            "message", success ? "过期消息清理成功" : "清理失败"
        ));
    }
}